package org.crumbleworks.crumbchat.client.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

import org.crumbleworks.crumbchat.client.controller.ClientControllerInterface;

public class MenuBar extends JMenuBar implements ActionListener {
    private static final long serialVersionUID = 1L;

    private ClientControllerInterface controller;

    private JMenu fileMenu, helpMenu;
    private JMenuItem connectItem, disconnectItem, closeItem, aboutItem;

    public MenuBar(ClientControllerInterface controller) {
        this.controller = controller;
        initMenus();
        initMenuItems();

        setVisible(true);
    }

    private void initMenus() {
        fileMenu = new JMenu("File");
        add(fileMenu);

        helpMenu = new JMenu("Help");
        add(helpMenu);
    }

    private void initMenuItems() {
        // File
        connectItem = new JMenuItem("Connect");
        connectItem.addActionListener(this);
        fileMenu.add(connectItem);

        disconnectItem = new JMenuItem("Disconnect");
        disconnectItem.addActionListener(this);
        fileMenu.add(disconnectItem);

        fileMenu.add(new JSeparator(JSeparator.HORIZONTAL));

        closeItem = new JMenuItem("Close");
        closeItem.addActionListener(this);
        fileMenu.add(closeItem);

        // Help
        aboutItem = new JMenuItem("About");
        aboutItem.addActionListener(this);
        helpMenu.add(aboutItem);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        System.out.println(getTopLevelAncestor());

        if (ae.getSource().equals(connectItem)) {
            System.out.println("connect");
        }
        else if (ae.getSource().equals(disconnectItem)) {
            controller.disconnectFromServer(false);
        }
        else if (ae.getSource().equals(closeItem)) {
            controller.disconnectFromServer(true);
        }
        else if (ae.getSource().equals(aboutItem)) {
            System.out.println("about");
        }
    }
}
